package com.chms.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.chms.bean.UserBean;
import com.chms.dao.IUserDao;
import com.chms.pojo.Jbsult;
import com.chms.utility.ApplicationException;
import com.chms.utility.ExceptionMessage;
import com.chms.utility.QueryConstant;

public class UserDao extends BaseDao implements IUserDao {

	public boolean updatePassword(String userId, String password,
			String newPassword) throws ApplicationException {
		List lParamList = new ArrayList();
		lParamList.add(0, userId);
		lParamList.add(1, password);
		lParamList.add(2, newPassword);

		// Get the query by passing parameters
		String query = getQuery(QueryConstant.UPDATE_USER_PASSWORD, lParamList);
		boolean isUpdated=false;
		try {
			int rowCount = update(query);
			if (rowCount != 0)
					isUpdated = true;
		} catch (SQLException sqe) {
			throw new ApplicationException(
					ExceptionMessage.UPDATE_PASSWORD_UNABLE);
		}
		return isUpdated;
	}

	public boolean deleteUserOnId(String userId) throws ApplicationException {
		boolean isDeleted=false;
		List lParamList = new ArrayList();
		lParamList.add(0, userId);
		
		// Get the query by passing parameters
		String query = getQuery(QueryConstant.DELETE_USER, lParamList);
		int resultCount = 0;
		try {
			resultCount = delete(query);
			
			if (resultCount != 0) {
				isDeleted = true;
			} else
				isDeleted = false;
		} catch (SQLException sqe) {
			throw new ApplicationException(
					ExceptionMessage.UPDATE_PASSWORD_UNABLE);
		}
		return isDeleted;
	}

	public boolean saveUser(UserBean userBean) throws ApplicationException {
		return false;
	}

	public boolean updateUser(String userId, String userRole)
			throws ApplicationException {
		List lParamList = new ArrayList();
		lParamList.add(0, userId);
		lParamList.add(1, userRole);
		
		// Get the query by passing parameters
		String query = getQuery(QueryConstant.UPDATE_USER_ROLE, lParamList);
		boolean isUpdated=false;
		try {
			int rowCount = update(query);
			if (rowCount != 0)
					isUpdated = true;
		} catch (SQLException sqe) {
			throw new ApplicationException(
					ExceptionMessage.UPDATE_USER_ROLE_UNABLE);
		}
		return isUpdated;

	}

	public List<Jbsult> getAllUsers() throws ApplicationException {
		List<Jbsult> usersList=new ArrayList<Jbsult>();
		String query = QueryConstant.ALL_USERS;

		try {
			ResultSet rs = getResults(query);
			while (rs.next()) {
				Jbsult jbsult=new Jbsult();
				jbsult.setUserId(String.valueOf(rs.getInt("USER_LOGIN_ID")));
				jbsult.setfName(rs.getString("USER_FNAME"));
				usersList.add(jbsult);
			}
		} catch (SQLException sqe) {
			throw new ApplicationException(
					ExceptionMessage.USERS_UNABLE);
		}
		return usersList;
	}
}
